while
in Action¶Learning Outcomes
while
NOTES
This problem is a simple, three-loop problem with slightly more complicated glue code (e.g. shifting down to the red square from the row above).
Give the students opportunities to discuss the problem and map it out with a partner.
Show how each stage of the problem can be tested before moving on to the next stage.
Observe how starting on a red square colludes with a color-based loop condition, requiring that bit move of the square first. Or (as done in the solution), you can simply use bit.front_clear()
.
Explain that an important skill in programming is identifying the technical requirements of a problem. Often, when the problem is simple, the solution can be simple. If the problem changes to become more complex, the code will need to change also. Comparing across instances of a problem helps us understand the nature of the problem and decide how complicated the code needs to be.
For example, if one of the bridges had an empty space immediately after the bridge, bit.front_clear()
would not work, and you'd have to use a color-based loop condition. Add fix-different-bridge
to the script to demonstrate.
Talk through how the code needs to change to adapt to this new requirement.
The next two problems introduce the important concept of nested loops, so don't spend too much time on this one (maybe 10-15 minutes, leaving more time for the other two).
Code is almost always intended to run on many different inputs. Compare across possible inputs to determine the technical requirements of your code.
There is always a compromise: code that handles more kinds of inputs is more complex; code that handles fewer, specific inputs is more simple.
NOTES
Allow the students to discuss how they would approach this problem.
This solution can use a nested loop with not bit.is_green()
as the condition.
Or you can repeat the "cover side" while loop 4 times.
Break the problem down into abstract pieces: cover a side, repeat the cover-side activity.
Solve the cover-side problem.
Then consider how to repeat that step: loop or code repetition. What do the students see as advantages or disadvantages to each approach?
Point out that the students will learn additional techniques in the next two weeks that could change how they approach this specific problem (so don't make any strong conclusions yet!).
However, being able to formulate this problem as a nested loop is very good practice at crafting good boundary conditions and glue code. The next problem will require a nested loop, so using this simpler problem as warm-up is a good idea.
NOTES
This is another nested loop problem.
Point out that unlike cover.py
, where the number of sides was fixed at 4, here the number of "climbs" varies from problem to problem. Simply hard-coding 4 loops will not work. We need a nested loop.
What is the abstract purpose of the outer loop? What is it's condition?
What is the abstract purpose of the loop body? How do we break it down? What is the repeated pattern? (Rise and tread together) What are the boundary conditions?
while
loops